package com.zjsru.oneDay202211;

/**
 * @Author: CookLee
 * @Date: 2022/11/1
 *
 * 检查两个字符串数组是否相等
 *
 * 输入：word1 = ["ab", "c"], word2 = ["a", "bc"]
 * 输出：true
 * 解释：
 * word1 表示的字符串为 "ab" + "c" -> "abc"
 * word2 表示的字符串为 "a" + "bc" -> "abc"
 * 两个字符串相同，返回 true
 */
public class ArrayStringsAreEqual {
    
    /**
     * 模拟
     */
    public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
        StringBuilder sb1 = new StringBuilder();
        for (String s : word1) {
            sb1.append(s);
        }
        StringBuilder sb2 = new StringBuilder();
        for (String s : word2) {
            sb2.append(s);
        }
        return sb1.toString().equals(sb2.toString());
    }
    
    /**
     * 双指针
     */
    public boolean arrayStringsAreEqual2(String[] word1, String[] word2) {
        int n = word1.length, m = word2.length;
        int i = 0, j = 0, p = 0, q = 0;
        while (i < n && j < m) {
            if (word1[i].charAt(p++) != word2[j].charAt(q++)) {
                return false;
            }
            //检测字符串组的字符串长度，如果指针走到尾则进入下个字符串并且将指针归零
            if (p == word1[i].length()) {
                i++;
                p = 0;
            }
            if (q == word2[j].length()) {
                j++;
                q = 0;
            }
        }
        return i == n && j == m;
    }
    
    public static void main(String[] args) {
        ArrayStringsAreEqual stringsAreEqual = new ArrayStringsAreEqual();
        String[] word1 = new String[]{"ab", "c"};
        String[] word2 = new String[]{"a", "bc"};
        System.out.println(stringsAreEqual.arrayStringsAreEqual(word1, word2));
    }
}
